(function(){
	/** 三个 */
	var  T_P_AAA=1003,
	/** 三顺 */
	 T_P_LLL=1006,	
	/** 四个 */
	 T_P_AAAA=1010,
	/** 炸 */
	 T_P_F=1007,
	 T_P_A=1001,
	/** 对子 */
	 T_P_AA=1002,
	/** 单顺 */
	 T_P_L=1004,
	/** 双顺 */
	 T_P_LL=1005,
	/********************g下面具体类型*********************************/
	/** 单个 */
	 T_S_A=T_P_A,
	/** 对子 */
	 T_S_AA=T_P_AA,
	/** 单顺 */
	 T_S_L=T_P_L,
	/** 双顺 */
	 T_S_LL=T_P_LL,
	/** 大王炸 */
	 T_S_F_KK=1008,
	/** 普通炸 */
	 T_S_F_AAAA=1009,
	/** 四带单 */
	 T_S_AAAABC=1011,
	/** 四带双 */
	 T_S_AAAABB=1012,
	 /** 四带两对 */
	 T_S_AAAABBCC=1019,
	/** 三带0 */
	 T_S_AAA=1013,
	/** 三带单 */
	 T_S_AAAB=1014,	
	/** 三带双 */
	 T_S_AAABB=1015,		
	/** 飞机带0 */
	 T_S_LLL=1016,
	/** 飞机带单 */
	 T_S_LLLB=1017,
	/** 飞机带对(三顺带对) */
	 T_S_LLLBB=1018;
	
	function getC(id){
		return Card.getCard(id);
	}
	
	function sort(arrays){
		var i,j,tmp;
		for(i=0;i<arrays.length;i++){
			for(j=i+1;j<arrays.length;j++){
				var ti=parseInt(arrays[i]),
					tj=parseInt(arrays[j]);
				if(ti>tj){
					tmp=arrays[i];
					arrays[i]=arrays[j];
					arrays[j]=tmp
				}
			}
		}
		return arrays;
	}
	function getSType(cards){
		if(isPA(cards))return T_P_A;	
		if(isPAA(cards))return T_P_AA;
		if(isSAAA(cards))return T_S_AAA;
		if(isSAAAB(cards))return T_S_AAAB;
		if(isSAAABB(cards))return T_S_AAABB;
		if(isSAAAABC(cards))return T_S_AAAABC;
		if(isSAAAABB(cards))return T_S_AAAABB;
		if(isSAAAABBCC(cards))return T_S_AAAABBCC;
		if(isPL(cards))return T_P_L;
		if(isPLL(cards))return T_P_LL;
		if(isSLLL(cards))return T_S_LLL;
		if(isSLLLB(cards))return T_S_LLLB;
		if(isSLLLBB(cards))return T_S_LLLBB;
		if(isSKK(cards))return T_S_F_KK;
		if(isSFAAAA(cards))return T_S_F_AAAA;
		return -1;		
	}
	function isPA(cards){
		if(cards.length!=1)return false;
		getC(cards[0]);
		return true;
	}	
	function isPF(cards){
		if(!(cards.length!=2||cards.length!=4))return false;
		if(cards.length==2){
			return isSKK(cards);
		}else if(cards.length==4){
			return isSFAAAA(cards);
		}
		return false;
	}
	
	function isSKK(cards){
		if( cards.length!=2 )return false;
		var cont1=getC(cards[0]);
		var cont2=getC(cards[1]);
		if(cont1.value==Card.CARD_VALUE_KING&&cont2.value==Card.CARD_VALUE_WANG)return true;
		if(cont1.value==Card.CARD_VALUE_WANG&&cont2.value==Card.CARD_VALUE_KING)return true;
		return false;
	}
	
	function isSFAAAA(cards){
		if( cards.length!=4 )return false;
		return isFourSame(cards[0], cards[1], cards[2], cards[3]);
	}	
	
	function isPL(cards){
		if(cards.length<5)return false;
		//生成牌面值
		var _cards=[];
		for(var i=0;i<cards.length;i++){
			_cards[i]=getC(cards[i]);
		}
		//比较牌面值,看是否符合规则
		for(var i=0;i<_cards.length-1;i++){
			if(_cards[i].value>=Card.CARD_VALUE_A)
				return false;
			if(_cards[i].value+1!=_cards[i+1].value)return false;
		}
		return true;
	}
	
	function isPLL(cards){
		if(cards.length<6)return false;
		if(cards.length%2!=0)return false;
		//生成牌面值
		var _cards=[];
		for(var i=0;i<cards.length;i++){
			_cards[i]=getC(cards[i]);
		}
		//newCards=sort(cards);
		for(var i=0;i<_cards.length-2;i+=2){
			if(_cards[i].value>=Card.CARD_VALUE_A)return false;
			if(_cards[i].value!=_cards[i+1].value)return false;
			if(_cards.length-2>i){
				if(_cards[i].value+1!=_cards[i+2].value)return false;
			}
		}
		return true;
	}
	
	function isPLLL(cards){
		if(cards.length<6)return false;
		if(cards.length%3==0)return isSLLL(cards);
		else if(cards.length%4==0)return isSLLLB(cards);
		else if(cards.length%5==0)return isSLLLBB(cards);
		return false;
	}
	
	function isSLLL(cards){
		if(!(cards.length>=6&&cards.length%3==0))return false;
		var _cards=[];
		for(var i=0;i<cards.length;i++){
			_cards[i]=getC(cards[i]);
		}
		for(var i=0;i<_cards.length-3;i+=3){
			if(_cards[i].value>=Card.CARD_VALUE_A)return false;
			if(!(_cards[i].value==_cards[i+1].value&&_cards[i].value==_cards[i+2].value))return false;
			if(cards.length-3>i){
				if(_cards[i].value+1!=_cards[i+3].value)return false;
			}
		}
		return true;
	}
	
	function isSLLLB(cards){
		if(!(cards.length>=8&&cards.length%4==0))return false;
		if(isSLLL(cards))return false;
		var _cards=[];
		for(var i=0;i<cards.length;i++){
			_cards.push(getC(cards[i]));
		}
		//取三个相同的
		var list=[];
		for(var i=0;i<cards.length-2;i++){
			if(i+2>=cards.length)break;
			if(isThreeSame(cards[i], cards[i+1], cards[i+2])){
				var sames=[_cards[i], _cards[i+1], _cards[i+2]];
				i+=2;
				list.push(sames);
			}
		}
		//湊不成至少两个三顺,返回false
		if(cards.length<2)return false;
		var size=cards.length/4;
		//取得个数等于cards.length/4
		//缓存取得的顺子个数是否是正常的 :如:4445556667778889:配对为:4448 5558 6668 7779||5554 6664 7774 8889
		if(size==list.length){
			for(var i=0;i<list.length-1;i++){
				if(list[i][0].value>=Card.CARD_VALUE_A)return false;
				if(list[i][0].value+1!=list[i+1][0].value)return false;
			}
			return true;
		}else if(size<list.length) {
			//如果第一和第二个是顺子
			if(list[0][0].value+1==list[1][0].value){
				console.log("2222111")
				for(var i=0;i<list.length-2;i++){
					if(i+1>=list.length)return false;
					if(list[i][0].value>=Card.CARD_VALUE_A)return false;
					if(list[i][0].value+1!=list[i+1][0].value)return false;
				}
				return true
			}else if(list[list.length-1][0].value-1==list[list.length-2][0].value){
				console.log("22222222")
				//如果最后一个和最后两个是顺子
				for(var i=1;i<list.length-2;i++){
					if(i+1>=list.length)return false;
					if(list[i][0].value>=Card.CARD_VALUE_A)return false;
					if(list[i][0].value+1!=list[i+1][0])return false;
				}
				return true;
			}
		}
		return false;
	}
	/** 取得单位为三个的顺子 */
	function getAlongThree(cards){
		var aaaList=[];
		if(cards.length<3)return aaaList;
		for(var i=0;i<cards.length-2;i++){
			if(i+2>=cards.length)break;
			if(isThreeSame(cards[i], cards[i+1], cards[i+2])){
				sames=[cards[i], cards[i+1], cards[i+2]];
				i+=2;
				aaaList.push(sames);
			}
		}
		for(var i=aaaList.length-1;i>0;i--){
			if(aaaList[i][0]!=aaaList[i-1][0]+1)aaaList.splice(i,1);
		}
		return aaaList;
	}

	function isSLLLBB(cards){
		if(!(cards.length>=10&&cards.length%5==0))return false;
		if(isSLLL(cards))return false;
		var _cards=[];
		for(var i=0;i<cards.length;i++){
			_cards.push(getC(cards[i]));
		}
		//取三个相同的
		var aaaList=[];

		for(var i=0 ;i<_cards.length-2;i++){
			if(i+2>=cards.length)break;
			if(isThreeSame(cards[i], cards[i+1], cards[i+2])){
				sames=[_cards[i], _cards[i+1], _cards[i+2]];
				i+=2;
				aaaList.push(sames);
			}else if(isTwoSame(cards[i], cards[i+1])){
				//sames={cards[i], cards[i+1]};
				//aaList.add(sames);
				//暂无处理内容
				i++;
			}else{
				return false;
			}
		}
		for(var i=0;i<aaaList.length-1;i++){
			if(aaaList[i][0].value+1!=aaaList[i+1][0].value)return false;
		}
		return true;
	}
	
	function isPAA(cards){
		if ( cards == null || cards.length != 2 ) return false;
		return isTwoSame(cards[0], cards[1]);
	}

	function isPAAA(cards){
		// 三张包含三种情况
		// 1.单独三张
		// 2.三张带一张
		// 2.三张带一对
		switch (cards.length) {
		case 3:	return isSAAA(cards);
		case 4:  return isSAAAB(cards);
		case 5: 	return isSAAABB(cards);
		}
		return false;
	}
	/** 三带0 */
	function isSAAA(cards){
		return  cards.length ==3&&isThreeSame(cards[0], cards[1], cards[2]);
	}
	
	function isSAAAB(cards){
		if ( cards == null ||cards.length != 4 ) return false;
		if ( isFourSame(cards[0], cards[1], cards[2], cards[3]) ) return false;
		return isThreeSame(cards[0], cards[1], cards[2]) 
			 || isThreeSame(cards[0], cards[1], cards[3])
			 || isThreeSame(cards[0], cards[2], cards[3]) 
			 || isThreeSame(cards[1], cards[2], cards[3]);
	}

	function isSAAABB(cards){
		if (cards.length != 5 ) return false;		

		//3带双,前面两个牌面大小一定会相等
		var card1=getC(cards[0]);
		var card2=getC(cards[1]);
		if(card1.value!=card2.value)return false;
		
		//如时第2个跟第3个牌面大小不等,比较后面四个是否相等
		if(card2.value!=card2.value){
			return isThreeSame(cards[2], cards[3], cards[4]); 
		}
		//如果第二个和第三个相等,比较最后两个牌面大小是否相等
		return isTwoSame(cards[3], cards[4]);		
	}
	
	function isPAAAA(cards){
		if(cards.length==8)return isSAAAABBCC(cards);
		if(cards.length==6){
			if( isSAAAABC(cards))return true;
			else return isSAAAABB(cards);
		}
		return false;
	}
	
	function isSAAAABC(cards){
		if(cards.length!=6)return false;
		var card1=getC(cards[0]),
		 	card2=getC(cards[1]),
		 	card3=getC(cards[2]),
		 	card4=getC(cards[3]),
		 	card5=getC(cards[4]),
		 	card6=getC(cards[5]);		
		if(card1.value==card2.value){
			if(card2.value==card3.value)
				return card3.value==card4.value
					&&card6.value!=card5.value
					&&card3.value!=card5.value;
		}else if(card2.value==card3.value){
				return card3.value==card4.value
					&&card3.value==card5.value
					&&card5.value!=card6.value;
		}else {
			return card3.value==card4.value
				&&card3.value==card5.value
				&&card3.value==card6.value;
		}
		return false;
	}

	
	function isSAAAABB(cards){
		if(cards.length!=6)return false;
		var card1=getC(cards[0]);
		var card2=getC(cards[1]);
		//4带双,前面两个一定会相等
		if(card1.value!=card2.value)return false;
		var card3=getC(cards[2]);
		//如时第2个跟第3个不等,比较后面四个是否相等
		if(card1.value!=card3.value){
			return isFourSame(cards[2], cards[3], cards[4], cards[5]); 
		}
		//如果第二个和第三个不等,比较前面四个是否相等及最后两个是否相等
		return isFourSame(cards[0], cards[1], cards[2], cards[3])&&isTwoSame(cards[4], cards[5]); 
	}

	/** 四带两对 */
	function isSAAAABBCC(cards ) {
		if(cards.length!=8)return false;
		var card1=getC(cards[0]),
			card2=getC(cards[1]);
		//4带两对,前面两个一定会相等
		if(card1.value!=card2.value)return false;
		var card3=getC(cards[2]),
			card4=getC(cards[3]);
		//4带两对,两个一定会相等
		if(card3.value!=card4.value)return false;		

		var card5=getC(cards[4]),
			card6=getC(cards[5]),
			card7=getC(cards[6]),
			card8=getC(cards[7]);
		//如时第2个跟第3个不等,比较后面四个是否相等
		if(card1.value!=card3.value){
			if(card5.value==card6.value&&card5.value==card3.value&&card7.value==card8.value&&card5.value!=card7.value)return true;
			return card5.value==card6.value&&card5.value==card7.value&&card5.value==card8.value;
		}else{
			//前面四个相同,比较后面四个
			if(card5.value!=card6.value)return false;
			if(card7.value!=card8.value)return false;
			return true;
		}
	}
	
	/** 三个是否相同 */
	function isThreeSame(  card1 ,  card2 ,  card3 ) {
		var con1 = getC(card1),
		con2 = getC(card2),
		con3 = getC(card3);
		return con1.value == con2.value && con1.value == con3.value;
	}
	/** 二个是否相同 */
	function isTwoSame( card1 , card2 ) {
		var con1 = getC(card1);
		var con2 = getC(card2);
		return con1.value == con2.value;
	}
	/** 四个是否相同 */
	function isFourSame( card1 ,card2 ,card3 ,card4 ) {
		var con1 = getC(card1);
		var con2 = getC(card2);
		var con3 = getC(card3);
		var con4 = getC(card4);
		return con1.value == con2.value && con1.value == con3.value && con1.value == con4.value;
	}
	
	window.Rule={
		//能否出牌
		/** 
		 * perOutCards 对方出牌id数组
		 * myOutCards  己方出牌id数组
		 * */
		canOut:function(perOutCards,myOutCards){
			//排序
			var perOutCardsSort=sort(perOutCards),
			myOutCardsSort=sort(myOutCards),
			preType=getSType(perOutCardsSort),
			myType=getSType(myOutCardsSort);
			if(preType==-1)return false;
			if(myType==-1)return false;

			var cont1=getC(perOutCardsSort[0]);
			var cont2=getC(myOutCardsSort[0]);	
			
			if(preType==myType){
				if(perOutCardsSort.length!=myOutCardsSort.length)return false;
				switch(preType){
				case T_P_A://单个
				case T_P_L://顺子
				case T_P_AA://对子
				case T_P_LL://连对		
				case T_S_AAA://三带0
				case T_S_LLL://飞机带0
				case T_S_F_AAAA://炸
					return cont1.value<cont2.value;				
				case T_S_F_KK://对方是王炸
					return false;
				case T_S_AAAB:{//三个带1
					var cont3=getC(perOutCardsSort[2]);
					var cont4=getC(myOutCardsSort[2]);
					return cont3.value<cont4.value;
				}case T_S_AAABB:{//三个带对
					var cont3=getC(perOutCardsSort[2]);
					var cont4=getC(myOutCardsSort[2]);
					return cont3.value<cont4.value;	
				}case T_S_AAAABC:{//四个带1	
					var cont3=getC(perOutCardsSort[2]);
					var cont4=getC(myOutCardsSort[2]);
					return cont3.value<cont4.value;					
				}case T_S_AAAABB:{//四个带对	
					var cont3=getC(perOutCardsSort[3]);
					var cont4=getC(myOutCardsSort[3]);
					return cont3.value<cont4.value;					
				}case T_S_AAAABBCC:{//四个带两对
					if(isFourSame(perOutCardsSort[4], perOutCardsSort[5], perOutCardsSort[6], perOutCardsSort[4])){
						if(isFourSame(myOutCardsSort[4], myOutCardsSort[5], myOutCardsSort[6], myOutCardsSort[4])){
							return getC(perOutCardsSort[4]).value<getC(myOutCardsSort[4]).value;
						}
						if(isFourSame(myOutCardsSort[2], myOutCardsSort[3], myOutCardsSort[4], myOutCardsSort[5])){
							return getC(perOutCardsSort[4]).value<getC(myOutCardsSort[4]).value;
						}	
						if(isFourSame(myOutCardsSort[0], myOutCardsSort[1], myOutCardsSort[2], myOutCardsSort[3])){
							return getC(perOutCardsSort[4]).value<getC(myOutCardsSort[3]).value;
						}						
					}else if(isFourSame(perOutCardsSort[2], perOutCardsSort[3], perOutCardsSort[4], perOutCardsSort[5])){
						if(isFourSame(myOutCardsSort[4], myOutCardsSort[5], myOutCardsSort[6], myOutCardsSort[4])){
							return getC(perOutCardsSort[4]).value<getC(myOutCardsSort[4]).value;
						}
						if(isFourSame(myOutCardsSort[2], myOutCardsSort[3], myOutCardsSort[4], myOutCardsSort[5])){
							return getC(perOutCardsSort[4]).value<getC(myOutCardsSort[4]).value;
						}	
						if(isFourSame(myOutCardsSort[0], myOutCardsSort[1], myOutCardsSort[2], myOutCardsSort[3])){
							return getC(perOutCardsSort[4]).value<getC(myOutCardsSort[3]).value;
						}						
					}else if(isFourSame(perOutCardsSort[0], perOutCardsSort[1], perOutCardsSort[2], perOutCardsSort[3])){
						if(isFourSame(myOutCardsSort[4], myOutCardsSort[5], myOutCardsSort[6], myOutCardsSort[4])){
							return getC(perOutCardsSort[3]).value<getC(myOutCardsSort[4]).value;
						}
						if(isFourSame(myOutCardsSort[2], myOutCardsSort[3], myOutCardsSort[4], myOutCardsSort[5])){
							return getC(perOutCardsSort[3]).value<getC(myOutCardsSort[4]).value;
						}	
						if(isFourSame(myOutCardsSort[0], myOutCardsSort[1], myOutCardsSort[2], myOutCardsSort[3])){
							return getC(perOutCardsSort[3]).value<getC(myOutCardsSort[3]).value;
						}						
					}
					return false;
				}case T_S_LLLB://飞机带1
				case T_S_LLLBB:{//飞机带对
					var pre=getAlongThree(perOutCards);
					var my=getAlongThree(myOutCards);
					if(pre.length<1||my.length<1)return false;
					var c1=getC(pre[0][0]);
					var c2=getC(my[0][0]);				
					return c1.value<c2.value;
				}}
			}else if(preType==T_S_F_AAAA&&myType==T_S_F_KK){
				//对方是普通炸,我是王炸
				return true;
			}else if(preType==T_S_F_KK){
				//对方是王炸
				return false;
			}else if(myType==T_S_F_AAAA||myType==T_S_F_KK){
				//我是炸
				return true;
			}
			return false;			
		},
		getSType:function(cards){
			var nc=sort(cards);
			return getSType(nc)
		}		
	}
})();
(function(){
	var CARD_PATTERN = [ "♠", "♥", "♣","♦" ],
	CARD_NAME=[ "3", "4", "5", "6", "7", "8", "9", "0", "J", "Q", "K", "A", "2", "王", "王" ];
	function getCardName(cardId) {
		return CARD_PATTERN[parseInt(cardId%4)] + CARD_NAME[parseInt(cardId/4)];
	}
	function getValue(cardId) {
		return parseInt(cardId/4);
	}
	var id_w=52,id_k=57;
	Card={
		CARD_ID_WANG:id_w,/** 小王 */
		CARD_ID_KING:id_k,/** 大王 */		
		CARD_VALUE_WANG:parseInt(id_w/4),/** 小王 */
		CARD_VALUE_KING:parseInt(id_k/4),/** 大王 */
		CARD_VALUE_A:11  ,/** A的牌面值 */
		CARD_VALUE_2:12,/** 2的牌面值 */
		getCard:function(id){
			return {
				cardId:id,
				cardName:getCardName(id),
				value:getValue(id)
			}
		}
	}
	window.Card=Card;
})();
/*
(function(){
	var u =[ 0,1, 2,5, 6,8,9, 4, 10 ];
	var m =[ 16,17,18,20,21,22,24,25,26 ];
	u = [32, 33, 34, 35, 43 ,47];
	m = [1, 44, 45, 46,47, 48 ];
	print(u, m);
	function print(u,m){
		console.log("u:"+u.join(",")+":>>"+Rule.getSType(u));
		console.log("m:"+m.join(",")+":>>"+Rule.getSType(m));
		var s=[];
		s.push(Rule.canOut(u,m));
		console.log("结果:"+ s.join(","));
	}
})();*/